package com.ftg.learn.bus.lnterceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Guo
 * @date 2022/3/13
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
//    LoginInterceptor loginInterceptor() {
//        return new LoginInterceptor();
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                //拦截的路径
                .addPathPatterns("/bus/**")
                //不拦截的路径
                .excludePathPatterns("/bus/login/in")
                .excludePathPatterns("/bus/user/downLoadStuInfoExcel");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")//允许全部请求
                .allowedOriginPatterns("http://26.234.93.45")//允许跨域的请求方路径
                .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
                        "Access-Control-Request-Headers","Accept")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials","http://26.234.93.45:8080")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}
